package com.smartlink.app.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

/**
 * 获取经纬度
 * Created by Administrator on 2016/4/7.
 */
public class GpsUtils {

    private static GpsUtils instance;
    private Context context;
    private LocationManager locationManager;
    private double[] values = new double[]{0, 0};

    private GpsUtils() {
    }

    private GpsUtils(Context context) {
        this.context = context;
        init();
    }

    public static synchronized GpsUtils getInstance(Context context) {
        if (instance == null) {
            instance = new GpsUtils(context);
        }
        return instance;
    }


    private void init() {
        if (locationManager == null) {
            locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        }
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        setValues(location);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                setValues(location);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        });

    }

    private void setValues(Location location) {
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            values[0] = lat;
            values[1] = lng;

        }
    }

    /**
     * 获取经纬度  value[0]=lat value[1]=lng
     *
     * @return
     */
    public double[] getValues() {
        return values;
    }
}

